package com.fancy.mobileschool.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fancy.mobileschool.R;
import com.fancy.mobileschool.entity.Course;

import java.util.List;

/**
 * Created by Jackie on 2017/11/16.
 *
 */

public class CourseSelectDialog extends Dialog {

    private Context context;

    private List<Course> courses;

    private CourseSelectedListener listener;

    private boolean selectName;

    public CourseSelectDialog(@NonNull Context context, List<Course> courses, CourseSelectedListener listener, boolean selectName) {
        super(context);
        this.listener = listener;
        this.courses = courses;
        this.context = context;
        this.selectName = selectName;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.course_select_layout, null, false);
        for (int i = 0 ; i < courses.size() ; i++ ) {
            View view = LayoutInflater.from(context).inflate(R.layout.course_dir_item, linearLayout, false);
            if (selectName)
                view.setTag(courses.get(i).name);
            else
                view.setTag(courses.get(i).course_id);


            final TextView courseName = (TextView) view.findViewById(R.id.course_dir_name);
            courseName.setText(courses.get(i).name);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.courseSelect((String) view.getTag());
                    dismiss();
                }
            });
            linearLayout.addView(view);
        }

        setContentView(linearLayout);
    }

    public interface CourseSelectedListener {

        void courseSelect(String courseTag);

    }
}
